package com.healthyeating.manage.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.healthyeating.common.annotation.Log;
import com.healthyeating.common.core.controller.BaseController;
import com.healthyeating.common.core.domain.AjaxResult;
import com.healthyeating.common.enums.BusinessType;
import com.healthyeating.manage.domain.Nutrition;
import com.healthyeating.manage.service.INutritionService;
import com.healthyeating.common.utils.poi.ExcelUtil;
import com.healthyeating.common.core.page.TableDataInfo;

/**
 * 成分列表Controller
 * 
 * @author 段奇乐
 * @date 2025-01-03
 */
@RestController
@RequestMapping("/manage/nutrition")
public class NutritionController extends BaseController
{
    @Autowired
    private INutritionService nutritionService;

    /**
     * 查询成分列表列表
     */
    @PreAuthorize("@ss.hasPermi('manage:nutrition:list')")
    @GetMapping("/list")
    public TableDataInfo list(Nutrition nutrition)
    {
        startPage();
        List<Nutrition> list = nutritionService.selectNutritionList(nutrition);
        return getDataTable(list);
    }

    /**
     * 导出成分列表列表
     */
    @PreAuthorize("@ss.hasPermi('manage:nutrition:export')")
    @Log(title = "成分列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Nutrition nutrition)
    {
        List<Nutrition> list = nutritionService.selectNutritionList(nutrition);
        ExcelUtil<Nutrition> util = new ExcelUtil<Nutrition>(Nutrition.class);
        util.exportExcel(response, list, "成分列表数据");
    }

    /**
     * 获取成分列表详细信息
     */
    @PreAuthorize("@ss.hasPermi('manage:nutrition:query')")
    @GetMapping(value = "/{nutritionId}")
    public AjaxResult getInfo(@PathVariable("nutritionId") Long nutritionId)
    {
        return success(nutritionService.selectNutritionByNutritionId(nutritionId));
    }

    /**
     * 新增成分列表
     */
    @PreAuthorize("@ss.hasPermi('manage:nutrition:add')")
    @Log(title = "成分列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Nutrition nutrition)
    {
        return toAjax(nutritionService.insertNutrition(nutrition));
    }

    /**
     * 修改成分列表
     */
    @PreAuthorize("@ss.hasPermi('manage:nutrition:edit')")
    @Log(title = "成分列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Nutrition nutrition)
    {
        return toAjax(nutritionService.updateNutrition(nutrition));
    }

    /**
     * 删除成分列表
     */
    @PreAuthorize("@ss.hasPermi('manage:nutrition:remove')")
    @Log(title = "成分列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{nutritionIds}")
    public AjaxResult remove(@PathVariable Long[] nutritionIds)
    {
        return toAjax(nutritionService.deleteNutritionByNutritionIds(nutritionIds));
    }
}
